<div th:fragment="content">
	<div class="homepage home-section text-center">
	    <div class="welcome-text">
			<h2>GIT 工具集</h2>
			<div class="subscribe-form">
	             <div class="row">
	               <input id="startGenerate" type="button" class="button default" value="创建 ..."/>
	               <input id="startDownload" type="button" class="button" value="下载 ..."/>
	               <input id="startPivotTable" type="button" class="button" value="分析 ..."/>
	             </div>
			</div>
	    </div>
	</div>
</div>

<div th:fragment="menu">
	<li>
		<a class="active button-home" href="javascript:void(0)"><i class="fa fa-home"></i>Home</a>
	</li>
</div>

<div th:fragment="script">
	<script>
	(function($) {
		"use strict";
		
		var FILE_NAME_STORAGE_KEY = "export-file-name";

	    $(".main-menu a.button-home").click(function() {
		    $("#menu-container .content").slideUp("slow");
		    $("#menu-container .homepage").slideDown("slow");
		    $(".logo-top-margin").animate({ marginLeft: "40%" }, "slow");
		    $(".logo-top-margin").animate({ marginTop: "20px" }, "slow");
		    return false;
		});
	    
	    $("#startGenerate").click(function(){
	    	var repos = $.settingRepoPaths()||"";
	    	if (! repos.trim()) {
	    		alert("未设置 本地 Repo 路径");
	    		return;
	    	}
	    	
	    	var fileType = $.settingExportType();
	    	var detailLevel = $.settingDetailLevel();
	    	
	    	var extGroovy = $.settingExtGroovyFile()||"";
	    	
	    	var confirmMsg =
	    		"确认使用以下参数生成数据文件:\n"+
	    		"导出文件类型: "+fileType+"\n"+
	    		"数据明细程度: "+detailLevel+"\n"+
	    		"本地 Repo 路径: \n"+repos;
	    	if (extGroovy){
	    		confirmMsg+="\n扩展 Groovy 脚本文件:\n"+extGroovy;
	    	}
	    	
	    	var result = confirm(confirmMsg);
	    	if (!result){
	    		return;
	    	}
	    	
			$.ajax({
				type : "post",
				url : "./service/export-stat/"+fileType+"/"+detailLevel,
				contentType : 'application/json',
				data : JSON.stringify({
					pathLines: repos,
					extScriptFile: extGroovy
				}),
				dataType : 'json',
				success : function(d) {
					if (d.success){
						$.jStorage.set(FILE_NAME_STORAGE_KEY, d.downloadAddress)
						var msg = "已导出到文件: " + d.downloadAddress + "\n\n" + d.message;
						alert(msg);
					}else{
						if (d.message){
							alert("程序运行错误 - "+d.message);
						}else{
							alert("程序运行错误 - "+JSON.stringify(d));
						}
					}
				},
				error: function(xhr, status, error){
			        var errorMessage = xhr.status + ': ' + xhr.statusText
			        alert('系统错误 - ' + errorMessage);
			        if (xhr.responseText){
			        	throw xhr.responseText;
			        }
			    }
			});
		});

		$("#startDownload").click(function() {
			var fileName = $.jStorage.get(FILE_NAME_STORAGE_KEY);
			if (! fileName){
				alert("未导出数据文件, 请先执行 ["+$("#startGenerate").val()+"]");
				return;
			}
			var result = confirm("确认下载数据文件: "+fileName);
	    	if (!result){
	    		return;
	    	}
	    	window.open("/file/"+fileName);
		});

		$("#startPivotTable").click(function() {
			var fileName = $.jStorage.get(FILE_NAME_STORAGE_KEY);
			if (! fileName){
				alert("未导出数据文件, 请先执行 ["+$("#startGenerate").val()+"]");
				return;
			}
            if ( !(fileName.endsWith(".csv")) ){
        	    alert("导出数据文件不是 CVS 格式(扩展名不是 '.csv'), 请到 'Settings' 中设置 '导出文件类型', 重新创建.");
        	    return;
            }
			var result = confirm("确认分析数据文件: "+fileName);
	    	if (!result){
	    		return;
	    	}
	    	window.open("/pivottable.js/pivottable.html#"+fileName, document.title+"/PivotTable/"+fileName);
		});
	})(jQuery);
	</script>
</div>
